package com.example.controller;

import com.example.job.JobService;
import com.example.job.TaskJob;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
public class SysJobController {

    Logger logger = LoggerFactory.getLogger(getClass());

    @Resource
    private JobService jobService;

    /***
     * 创建一个延时任务
     * @param id
     * @param time
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "createAndSetJob")
    public boolean createAndSetJob(String id, int time) throws Exception {
        logger.warn("创建临时任务: " + id + " 延时时间: " + time);
        return jobService.createAndSetJobInTx(id, TaskJob.class, time);
    }

    /***
     * 创建一个可重复执行的任务
     * @param id 任务id 用于任务的取消
     * @param time 间隔时间 单位： 秒
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "createAndSetRepeatJob")
    public boolean createAndSetRepeatJob(String id, int time) throws Exception {
        return jobService.createAndSetRepeatJobInTx(id, TaskJob.class, time);
    }

    /****
     * 保存更新定时任务
     * @param id 任务id
     * @param time 间隔时间
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "updateJob")
    public boolean updateJob(String id, int time) throws Exception {
        return jobService.updateJobInTx(id, time);
    }

    /***
     * 取消任务
     * @param id 任务id
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "cancelJob")
    public boolean cancelJob(String id) throws Exception {
        return jobService.cancelJobInTx(id);
    }
}
